package switchviz.core.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import switchviz.core.ResourceManager;
import switchviz.graphic.gauge.IGauge;
import switchviz.graphic.signal.IGaugeSignal;
import switchviz.gui.component.PaddingPanel;

public class ViewLegend extends JFrame implements WindowListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3715632767443467668L;
	private View _view;
	
	public ViewLegend(View view)
	{
		this._view = view;
		initialize();
		this.setTitle("Legend of " + _view.getDataModel().getName());
		this.setIconImage(ResourceManager.getInstance().getImageIconLegend().getImage());
		this.pack();
		this.setLocationRelativeTo(null);
		this.addWindowListener(this);
		this.setVisible(true);
	}
	
	private void initialize()
	{
		PaddingPanel panel = new PaddingPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		String lastname = "";
		for (IGauge gauge : _view.getGaugeArray())
		{
			String newname = gauge.getGaugeDataModel().getName() + gauge.getGaugeDataModel().getType() + gauge.getSignals().length;
			if(!gauge.getGaugeDataModel().isMultiplePerPort() || !newname.equals(lastname))
			{
				lastname = newname;
				JLabel label = new JLabel(gauge.toString());
				Font labelfont = label.getFont();
				labelfont = labelfont.deriveFont(Font.BOLD, 14.0f);
				label.setFont(labelfont);
				panel.add(label);
				for(IGaugeSignal signal : gauge.getVisibleSignals())
				{
					JPanel signalpanel = new JPanel(ResourceManager.getInstance().getBorderLayout());
					JLabel color = new JLabel();
					color.setOpaque(true);
					color.setBackground(new Color(signal.getColor()[0], signal.getColor()[1], signal.getColor()[2]));
					color.setPreferredSize(ResourceManager.getInstance().getConfigurationLabelDimension());
					color.setBorder(BorderFactory.createLineBorder(Color.black, 2));
					JLabel signame = new JLabel(signal.getName());
					signalpanel.add(color, BorderLayout.WEST);
					signalpanel.add(signame, BorderLayout.CENTER);
					panel.add(signalpanel);
				}
				panel.add(new JLabel(" "));
			}
		}
		this.setContentPane(panel);
	}
	
	public void windowClosed(WindowEvent evt) 
	{
		this.setVisible(false);
	}
	
	public void windowClosing(WindowEvent evt) 
	{
		this.setVisible(false);
	}
	
	public void windowActivated(WindowEvent arg0) {}
	public void windowDeactivated(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}
	public void windowOpened(WindowEvent arg0) {}
}
